{########################}
{### Comment Locating ###}
{########################}
{% if page.comments or post.comments %}
<script type="text/javascript">
if(window.location.hash){
    var checkExist = setInterval(function() {
        if ($(window.location.hash).length) {
            $('html, body').animate({scrollTop: $(window.location.hash).offset().top-90}, 1000);
            clearInterval(checkExist);
        }
    }, 100);
}
</script>
{% endif %}


{###################}
{### Minigrid JS ###}
{###################}
<script type="text/javascript">
/* @license Minigrid v3.1.1 – https://cdn.jsdelivr.net/npm/minigrid@3.1.1/dist/minigrid.min.js */
!function(t, e) {
	"function" == typeof define && define.amd ? define(e) : "object" == typeof exports ? module.exports = e() : t.Minigrid = e()
}(this, function(t) {
	"use strict";
	function e(t, e) {
		for (var i in e) e.hasOwnProperty(i) && (t[i] = e[i]);
		return t
	}
	var i = function(t) {
			var i = t.container instanceof Node ? t.container : document.querySelector(t.container),
				r = t.item instanceof NodeList ? t.item : i.querySelectorAll(t.item);
			this.props = e(t, {
				container: i,
				nodeList: r
			})
		};
	return i.prototype.mount = function() {
		if (!this.props.container) return !1;
		if (!this.props.nodeList || 0 === this.props.nodeList.length) return !1;
		var t = "number" == typeof this.props.gutter && isFinite(this.props.gutter) && Math.floor(this.props.gutter) === this.props.gutter ? this.props.gutter : 0,
			e = this.props.done,
			i = this.props.container,
			r = this.props.nodeList;
		i.style.width = "";
		var n = Array.prototype.forEach,
			o = i.getBoundingClientRect().width,
			s = r[0].getBoundingClientRect().width + t,
			p = Math.max(Math.floor((o - t) / s), 1),
			a = 0;
		o = s * p + t + "px", i.style.width = o, i.style.position = "relative";
		for (var c = [], u = [], l = 0; l < p; ++l) u.push(l * s + t), c.push(t);
		this.props.rtl && u.reverse(), n.call(r, function(e) {
			var i = c.slice(0).sort(function(t, e) {
				return t - e
			}).shift();
			i = c.indexOf(i);
			var r = parseInt(u[i]),
				n = parseInt(c[i]);
			e.style.position = "absolute", e.style.webkitBackfaceVisibility = e.style.backfaceVisibility = "hidden", e.style.transformStyle = "preserve-3d", e.style.transform = "translate3D(" + r + "px," + n + "px, 0)", c[i] += e.getBoundingClientRect().height + t, a += 1
		}), i.style.display = "";
		var f = c.slice(0).sort(function(t, e) {
			return t - e
		}).pop();
		i.style.height = f + "px", "function" == typeof e && e(r)
	}, i
});
</script>


{####################}
{### Friends Grid ###}
{####################}
{% if page.type in ['friends'] %}
<script type="text/javascript">
// friends repo CDN
var FriendsPath = 'https://cdn.jsdelivr.net/gh/lei2rock/Friends';
// friends list file
var FriendsList = FriendsPath + '/friendslist.json';
// friends website logo folder
var LogoPath = FriendsPath + '/logo/';
// generate friends link grid
friend = {
    init: function () {
        var that = this;
        $.getJSON(FriendsList, function (data) {
            that.render(data);
        });
    },
    render: function (data) {
        var name, logo, url, li = "";
        for (var i = 0; i < data.length; i++) {
            name = data[i].name;
            logo = data[i].logo;
            url = data[i].url;
            li += '<div class="card">' +
                    '<a href="' + url + '" target="_blank">' +
                        '<div class="thumb" style="background: url(' + LogoPath + logo + ');">' + '</div>' +
                    '</a>' +
                    '<div class="card-header">' +
                        '<div class="nowrap"><a href="' + url + '" target="_blank">' + name + '</a></div>' +
                    '</div>' +
                    '</div>';
        }
        $(".FriendsGrid").append(li);
    }
}
friend.init();
</script>
{% endif %}


{###################}
{### Photos Grid ###}
{###################}
{% if page.type in ['photos'] %}
<script type="text/javascript">
// photos list path
var imgDataPath = 'https://cdn.jsdelivr.net/gh/lei2rock/PhotoGallery/photoslist.json';
// photos storage path
var imgPath = 'https://website-1256060851.file.myqcloud.com/pages/photos/';
// max photo number
var imgMaxNum = 50;
// window width (to decide photo width)
var windowWidth = window.innerWidth
|| document.documentElement.clientWidth
|| document.body.clientWidth;
if (windowWidth < 768) {
    var imageWidth = 145; // photo width (mobile)
} else {
    var imageWidth = 210; // photo width
}

// Tencent Cloud imgage style (based on photo width)
var imgStyle = '!' + imageWidth + 'x';

// generate photo grid
var LinkDataPath = imgDataPath;
photo = {
    page: 1,
    offset: imgMaxNum,
    init: function () {
        var that = this;
        $.getJSON(LinkDataPath, function (data) {
            that.render(that.page, data);
        });
    },
    render: function (page, data) {
        var begin = (page - 1) * this.offset;
        var end = page * this.offset;
        if (begin >= data.length) return;
        var imgNameWithPattern, imgName, imageSize, imageX, imageY, li = "";
        for (var i = begin; i < end && i < data.length; i++) {
            imgNameWithPattern = data[i].split(' ')[1];
            imgName = imgNameWithPattern.split('.')[0]
            imageSize = data[i].split(' ')[0];
            imageX = imageSize.split('.')[0];
            imageY = imageSize.split('.')[1];
            li += '<div class="card" style="width:' + imageWidth + 'px" >' +
                    '<div class="ImageInCard" style="height:'+ imageWidth * imageY / imageX + 'px">' +
                        '<a data-fancybox="gallery" href="' + imgPath + imgNameWithPattern + '" data-caption="' + imgName + '" title="' +  imgName + '">' +
                            '<img data-src="' + imgPath + imgNameWithPattern + imgStyle + '" src="' + imgPath + imgNameWithPattern + imgStyle + '" data-loaded="true">' +
                        '</a>' +
                    '</div>' +
                    '</div>'
        }
        $(".PhotoGrid").append(li);
        this.minigrid();
    },
    minigrid: function() {
        var grid = new Minigrid({
            container: '.PhotoGrid',
            item: '.card',
            gutter: 12
        });
        grid.mount();
        $(window).resize(function() {
            grid.mount();
        });
    }
}
photo.init();
</script>
{% endif %}